package com.blt.govdata.controller;

import com.baomidou.mybatisplus.entity.Column;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TSysRole;
import com.blt.govdata.service.ITSysMenuService;
import com.blt.govdata.service.ITSysRoleMenuService;
import com.blt.govdata.service.ITSysRoleService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色管理前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tSysRole")
//todo 将所有的XXX修改为真实值
public class TSysRoleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITSysRoleService service;

    @Autowired
    private ITSysMenuService menuService;

    @Autowired
    private ITSysRoleMenuService roleMenuService;

    /**
     * 分页查询角色
     * size:分页大小
     * current:当前页
     */
    @RequestMapping("/page")
    public Map<String, Object> getUsersPage(String keyword, Page<TSysRole> page) {
        Page<TSysRole> result = this.service.selectPage(page, new EntityWrapper<TSysRole>().where("in_use", Constants.UN_DELETED).like("name", keyword));
        logger.info("分页查询用户");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 新增角色
     */
    @PostMapping("/addRole")
    public Map<String, Object> addRole(TSysRole sysRole) {
        boolean result = this.service.insert(sysRole);
        logger.info("新增角色");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑角色
     */
    @PostMapping("/editRole")
    public Map<String, Object> editRole(TSysRole sysRole) {
        boolean result = this.service.updateById(sysRole);
        logger.info("编辑角色");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除角色
     */
    @PostMapping("/deleteRole")
    public Map<String, Object> deleteRole(TSysRole sysRole) {
        if (null == sysRole || sysRole.getId() == null) return ImmutableMap.of("status", "false", "data", false);
        sysRole.setInUse(Constants.IS_DELETED); //逻辑删除
        boolean result = this.service.updateById(sysRole);
        logger.info("删除角色");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 根据Id查询角色
     */
    @RequestMapping(value = "/selectRoleById", params = {"id"})
    public Map<String, Object> selectRoleById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }

    /**
     * 根据角色查询菜单的ID
     */
    @RequestMapping(value = "/selectRoleMenus", params = {"id"})
    public Map<String, Object> selectUserRoles(Integer id) {
        List<Map<String, Integer>> menuFids = menuService.selectMaps(BeanUtils.instantiate(EntityWrapper.class).setSqlSelect("id").where("pid = {0}", 0));
        List<Integer> fids = menuFids.stream().map(e -> e.get("id")).collect(Collectors.toList());
        List<Map<String, Integer>> list = this.roleMenuService.selectMaps(BeanUtils.instantiate(EntityWrapper.class)
                .setSqlSelect(BeanUtils.instantiate(Column.class).column("m_id").as("mId"))
                .where("r_id = {0}", id)
                .notIn("m_id", fids));
        if (CollectionUtils.isEmpty(list)) return ImmutableMap.of("status", "true", "data", Lists.newArrayList());

        List<Integer> mIds = list.stream().map(e -> e.get("mId")).collect(Collectors.toList());

        return ImmutableMap.of("status", "true", "data", mIds);
    }

}
